package simple.form.dao;

import simple.form.utils.DateUtil;
import simple.form.utils.SafeConvertUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 实体类对应的表属性
 * Created by Administrator on 2017/3/1.
 */
public class Table {

    public static final int BYTE =0;
    public static final int SHORT =1;
    public static final int INT =2;
    public static final int  LONG=3;
    public static final int FLOAT =4;
    public static final int DOUBLE =5;
    //    public static final int CHART =6;
    public static final int BOOLEAN =7;

    public static final int BYTE_CLASS =8;
    public static final int SHORT_CLASS =9;
    public static final int INT_CLASS =10;
    public static final int LONG_CLASS=11;
    public static final int FLOAT_CLASS =12;
    public static final int DOUBLE_CLASS =13;
    //    public static final int CHART_CLASS =14;
    public static final int BOOLEAN_CLASS =15;

    public static final int STRING =16;

    public static final int DATE = 17;
    /**
     * 请求参数的数据类型
     */
    public static final Map<String,Integer> type = new HashMap<String,Integer>(){{
        put("byte",BYTE);
        put("short",SHORT);
        put("int",INT);
        put("long",LONG);
        put("float",FLOAT);
        put("double",DOUBLE);
//        put("char",CHART);
        put("boolean",BOOLEAN);

        put("java.lang.Byte",BYTE_CLASS);
        put("java.lang.Short",SHORT_CLASS);
        put("java.lang.Integer",INT_CLASS);
        put("java.lang.Long",LONG_CLASS);
        put("java.lang.Float",FLOAT_CLASS);
        put("java.lang.Double",DOUBLE_CLASS);
//        put("class java.lang.Character",CHART_CLASS);
        put("java.lang.Boolean",BOOLEAN_CLASS);

        put("java.lang.String",STRING);

        put("java.util.Date",DATE);
    }};
    private String tableName;
    private String key;



    /*Map<"字段名称","字段类型">*/
    private Map<String,Class> fields;

    public Table(String key, String tableName) {
        this.key = key;
        this.tableName = tableName;
        this.fields = new HashMap<>();
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Map<String, Class> getFields() {
        return fields;
    }

    public void setFields(Map<String, Class> fields) {
        this.fields = fields;
    }

    public void addField(String fieldName, Class fieldType) {
        this.fields.put(fieldName,fieldType);
    }

    /**
     * 结果集类型转换
     * @param result
     * @param resultType
     * @return
     */
    public static Object convertResultSet(String result, Class resultType) {

        int dataType = type.get(resultType.getName());
        switch (dataType){
            case BYTE:
                return SafeConvertUtil.toByte(result);
            case SHORT:
                return SafeConvertUtil.toShort(result);
            case INT:
                return SafeConvertUtil.toInteger(result);
            case LONG:
                return SafeConvertUtil.toLong(result);
            case FLOAT :
                return SafeConvertUtil.toFloat(result);
            case DOUBLE :
                return SafeConvertUtil.toDouble(result);
            case BOOLEAN :
                return SafeConvertUtil.toBoolean(result);
            case BYTE_CLASS :
                return SafeConvertUtil.toByte(result);

            case SHORT_CLASS:
                return SafeConvertUtil.toShort(result);
            case INT_CLASS:
                return SafeConvertUtil.toInteger(result);
            case LONG_CLASS:
                return SafeConvertUtil.toLong(result);
            case FLOAT_CLASS :
                return SafeConvertUtil.toFloat(result);
            case DOUBLE_CLASS :
                return SafeConvertUtil.toDouble(result);
            case BOOLEAN_CLASS :
                return SafeConvertUtil.toBoolean(result);
            case STRING:
                return SafeConvertUtil.toString(result);
            case DATE:
                return DateUtil.parse(result,"yyyy-MM-dd");
            default:
                return null;
        }
    }
}
